package com.sykj.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.pojo.Role;
import com.sykj.pojo.User;
import com.sykj.servies.LoginServies;
import com.sykj.servies.RoleServies;
import com.sykj.servies.UserRoleServies;
import com.sykj.util.Page;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/config")
public class UserController {
    //管理员界面
    @Autowired
    LoginServies loginServies;
    //角色
    @Autowired
    RoleServies roleServies;
    //权限
    @Autowired
    UserRoleServies userRoleService;
    //查询
    @RequestMapping("/listUser")
    public  String list(Model model, Page page){
        //分页查询
        PageHelper.offsetPage(page.getStart(),page.getCount());
        //查询数据给集合
        List<User> list=loginServies.list();
        //查出总条数
        int total = (int) new PageInfo<>(list).getTotal();
        //赋值
        page.setTotal(total);
        //传值
        model.addAttribute("list",list);
        model.addAttribute("total",total);
        //查出该管理员拥有什么角色
        Map<User,List<Role>> userroles = new HashMap<>();
        //每个用户对应的角色
        for (User user : list) {
            //查出管理员对应的角色
            List<Role> roles=roleServies.listRoles(user);
            userroles.put(user, roles);
        }
        model.addAttribute("userroles", userroles);

        return "syspage/admin-list";
    }

    //启用
    @RequestMapping("/enableStatus")
    @ResponseBody
    public String enableStatus(@RequestParam(value = "name") String name){
        return loginServies.enableStatus(name);
    }
    //停用
    @RequestMapping("/stopStatus")
    @ResponseBody
    public String stopStatus(@RequestParam(value = "name") String name){
        return loginServies.stopStatus(name);
    }
    //删除
    @RequestMapping("/deleteUser")
    public String del(Model model,Long id){
        loginServies.delete(id);
        //重定向到当前页，刷新
        return "redirect:listUser";
    }

   //点击编辑跳转到管理员修改界面
    @RequestMapping("/editUser")
    public String edit(Model model,Long id){
        //查到所有角色，便于修改
        List<Role> rs = roleServies.list();
        model.addAttribute("rs", rs);
        //得到当前登录的管理员id
        User user =loginServies.get(id);
        model.addAttribute("user", user);
        //当前当前管理员拥有的角色
        List<Role> roles =roleServies.listRoles(user);
        model.addAttribute("currentRoles", roles);

        return "syspage/admin-edit";
    }
    //修改管理员
    @RequestMapping("/updateUser")
    public String update(User user, long[] roleIds){
        //给当前登录的管理员传入角色
        userRoleService.setRoles(user,roleIds);
        //得到密码
        String password=user.getPassword();
        //如果在修改的时候没有设置密码，就表示不改动密码
        if(user.getPassword().length()!=0) {
            //自动生成随机数，盐值加密
            String salt = new SecureRandomNumberGenerator().nextBytes().toString();
            //加密连词
            int times = 2;
            String algorithmName = "md5";
            String encodedPassword = new SimpleHash(algorithmName,password,ByteSource.Util.bytes(user.getName()),times).toString();
            user.setSalt(salt);
            user.setPassword(encodedPassword);
            //user.setStatus(1);
        }
        else
            //密码为空
            user.setPassword(null);
        //修改当前管理员
        loginServies.update(user);
        //重定向到当前页，刷新
        return "redirect:listUser";

    }

    //点击增加跳到增加管理员页面
    @RequestMapping("/adminAdd")
    public String adminadd(Model model){
        //把角色传入到下拉框
        List<Role> list = roleServies.list();
        model.addAttribute("rolelist",list);
        return "syspage/admin-add";
    }

    //增加管理员
   @RequestMapping("/addUser")
    public String add(User user,long[] roleIds){
        //生成随机数
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        int times = 2;
        String algorithmName = "md5";
        //密码加用户名加密
        String encodedPassword = new SimpleHash(algorithmName,user.getPassword(), ByteSource.Util.bytes(user.getName()),times).toString();

        User u = new User();
        u.setName(user.getName());
        //盐值加密
        u.setPassword(encodedPassword);
        u.setSalt(salt);
        //状态默认给1==启用
        u.setStatus(1);
        u.setAddress(user.getAddress());
        u.setPhone(user.getPhone());
        u.setLasttime(new Date());
        loginServies.add(u);
        //
        userRoleService.setRoles(u,roleIds);
        //重定向，刷新
        return "redirect:listUser";
    }
}
